package com.itheima.mp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mp.domain.po.OrdersMpapi;
import com.itheima.mp.mapper.OrdersMpapiMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersMpapi")
public class OrdersMpapiMapperapiController {

    @Autowired
    private OrdersMpapiMapper ordersMpapiMapper;

    // 1. 添加订单
    @PostMapping("/create")
    public ResponseEntity<?> ordersMpapi_createOrder(@RequestBody OrdersMpapi order) {
        int result = ordersMpapiMapper.insert(order);
        if (result > 0) {
            return ResponseEntity.ok("Order created successfully");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to create order");
        }
    }

    // 2. 更新订单状态
    @PutMapping("/updateStatus/{orderId}")
    public ResponseEntity<?> ordersMpapi_updateOrderStatus(@PathVariable String orderId, @RequestBody String newStatus) {
        OrdersMpapi order = new OrdersMpapi();
        order.setOrderId(orderId);
        order.setOrderStatus(newStatus);
        int result = ordersMpapiMapper.updateById(order);
        if (result > 0) {
            return ResponseEntity.ok("Order status updated successfully");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to update order status");
        }
    }
    // 2.1. 根据 Wrapper 条件修改记录
    @PatchMapping("/updateByWrapper/{orderId}")
    public ResponseEntity<?> ordersMpapi_updateByWrapper(@PathVariable String orderId, @RequestBody Map<String, Object> updateValues) {
        QueryWrapper<OrdersMpapi> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        int result = ordersMpapiMapper.update(null, queryWrapper);
        if (result > 0) {
            return ResponseEntity.ok("Order updated successfully based on conditions");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to update order based on conditions");
        }
    }

    // 3. 删除订单
    @DeleteMapping("/delete/{orderId}")
    public ResponseEntity<?> ordersMpapi_deleteOrder(@PathVariable String orderId) {
        int result = ordersMpapiMapper.deleteById(orderId);
        if (result > 0) {
            return ResponseEntity.ok("Order deleted successfully");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to delete order");
        }
    }
    // 3.1. 根据 Wrapper 条件删除记录
    @DeleteMapping("/deleteByWrapper/{orderId}")
    public ResponseEntity<?> ordersMpapi_deleteByWrapper(@PathVariable String orderId) {
        QueryWrapper<OrdersMpapi> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        int result = ordersMpapiMapper.delete(queryWrapper);
        if (result > 0) {
            return ResponseEntity.ok("Order deleted successfully based on conditions");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to delete order based on conditions");
        }
    }

    // 3.2. 批量删除记录
    @DeleteMapping("/deleteBatch")
    public ResponseEntity<?> ordersMpapi_deleteBatch(@RequestBody List<String> orderIds) {
        int result = ordersMpapiMapper.deleteBatchIds(orderIds);
        if (result > 0) {
            return ResponseEntity.ok("Orders deleted successfully in batch");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to delete orders in batch");
        }
    }

    // 4. 查询单个订单详情
    @GetMapping("/get/{orderId}")
    public ResponseEntity<?> ordersMpapi_getOrderDetails(@PathVariable String orderId) {
        OrdersMpapi order = ordersMpapiMapper.selectById(orderId);
        if (order != null) {
            return ResponseEntity.ok(order);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Order not found");
        }
    }

    // 5. 查询订单列表
    @GetMapping("/list")
    public ResponseEntity<?> ordersMpapi_listOrders(@RequestParam(required = false) String status,
                                                    @RequestParam(required = false) Long customerId) {
        QueryWrapper<OrdersMpapi> queryWrapper = new QueryWrapper<>();
        if (status != null) {
            queryWrapper.eq("order_status", status);
        }
        if (customerId != null) {
            queryWrapper.eq("customer_id", customerId);
        }
        List<OrdersMpapi> orders = ordersMpapiMapper.selectList(queryWrapper);
        return ResponseEntity.ok(orders);
    }

    // 5.1. 根据 Wrapper 条件查询记录数
    @GetMapping("/countByWrapper")
    public ResponseEntity<?> ordersMpapi_countByWrapper(@RequestParam(required = false) String status,
                                                        @RequestParam(required = false) Long customerId) {
        QueryWrapper<OrdersMpapi> queryWrapper = new QueryWrapper<>();
        if (status != null) {
            queryWrapper.eq("order_status", status);
        }
        if (customerId != null) {
            queryWrapper.eq("customer_id", customerId);
        }
        Long count = ordersMpapiMapper.selectCount(queryWrapper);
        return ResponseEntity.ok(count);
    }
    // 5.2. 根据多个客户ID批量查询订单
    @GetMapping("/batch")
    public ResponseEntity<?> ordersMpapi_batchGetOrdersByCustomerIds(@RequestParam("customerIds") List<Long> customerIds) {
        if (CollectionUtils.isEmpty(customerIds)) {
            return ResponseEntity.badRequest().body("Customer IDs are required");
        }
        List<OrdersMpapi> orders = ordersMpapiMapper.selectBatchIds(customerIds);
        if (CollectionUtils.isNotEmpty(orders)) {
            return ResponseEntity.ok(orders);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No orders found for the provided customer IDs");
        }
    }

    // 6. 分页查询订单
    @GetMapping("/page")
    public ResponseEntity<?> ordersMpapi_pageOrders(@RequestParam int page,
                                                    @RequestParam int size,
                                                    @RequestParam(required = false) String status) {
        Page<OrdersMpapi> pageParam = new Page<>(page, size);
        QueryWrapper<OrdersMpapi> queryWrapper = new QueryWrapper<>();
        if (status != null) {
            queryWrapper.eq("order_status", status);
        }
        Page<OrdersMpapi> orderPage = ordersMpapiMapper.selectPage(pageParam, queryWrapper);
        return ResponseEntity.ok(orderPage);
    }


}